﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Model;
using WorkOrder.Business;
using System.Drawing;

namespace WorkOrder
{
    public partial class showChangeover : System.Web.UI.Page
    {
        Business_Table_Changeover B_T_C = new Business_Table_Changeover();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
            
            if (!Page.IsPostBack)
            {
                #region 加载资料
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = B_T_C.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;

                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = B_T_C.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;

                ddlFactory.Items.Clear();
                ddlFactory.DataTextField = "fact_name";
                ddlFactory.DataValueField = "fact_id";
                ddlFactory.DataSource = B_T_C.GetFactory();
                ddlFactory.DataBind();
                ddlFactory.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlFactory.SelectedIndex = ddlFactory.Items.Count - 1;

                ddlSupervision.Items.Clear();
                ddlSupervision.DataTextField = "supe_unit_name";
                ddlSupervision.DataValueField = "supe_unit_id";
                ddlSupervision.DataSource = B_T_C.GetSupervisionUnit();
                ddlSupervision.DataBind();
                ddlSupervision.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlSupervision.SelectedIndex = ddlSupervision.Items.Count - 1;

                ddlIntegrated.Items.Clear();
                ddlIntegrated.DataTextField = "inte_unit_name";
                ddlIntegrated.DataValueField = "inte_unit_id";
                ddlIntegrated.DataSource = B_T_C.GetIntegratedUnit();
                ddlIntegrated.DataBind();
                ddlIntegrated.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlIntegrated.SelectedIndex = ddlIntegrated.Items.Count - 1;

                ddlDebuggion.Items.Clear();
                ddlDebuggion.DataTextField = "debug_unit_name";
                ddlDebuggion.DataValueField = "debug_unit_id";
                ddlDebuggion.DataSource = B_T_C.GetDebuggingUnit();
                ddlDebuggion.DataBind();
                ddlDebuggion.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlDebuggion.SelectedIndex = ddlDebuggion.Items.Count - 1;

                ddlStatus.Items.Clear();
                ddlStatus.DataTextField = "changeover_status";
                ddlStatus.DataValueField = "changeover_status_id";
                ddlStatus.DataSource = B_T_C.GetStatus();
                ddlStatus.DataBind();
                ddlStatus.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlStatus.SelectedIndex = ddlStatus.Items.Count - 1;
                #endregion         
      
                if (!string.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = Request.Params["id"];
                }
                LoadData();
            }
            
        }

        #region gridView的RowCommand事件
        protected void gvReport_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                if (new Business_Table_Permission().HasDelete(Session["UserID"].ToString(), "Table_Cute_log"))
                {
                    if (B_T_C.DeleteCutLog(e.CommandArgument.ToString()))
                    {
                        LoadData();
                    }
                }
                else
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "widowhref", "<script type='text/javascript'>alert('对不起，您没有删除此信息的权限！');</script>");
            }
        }
        #endregion
        
        #region 处理光标行颜色事件
        protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
                if (!string.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = Request.Params["id"];
                    Model_Table_Changeover model;
                    model = new Model_Table_Changeover();
                    model.Changeover_id = hidID.Value;
                    model.Order_number = Server.HtmlEncode(txtBusinessOrderNo.Value.Trim());
                    model.Area_id = seleArea.Value;
                    model.Customer_address = Server.HtmlEncode(txtCustomerAdd.Value.Trim());
                    model.Customer_name = Server.HtmlEncode(txtCustomer.Value.Trim());
                    model.Customer_tell = Server.HtmlEncode(txtCustomerTell.Value.Trim());
                    model.Debug_unit_id = ddlDebuggion.SelectedValue;
                    model.Equipment_model = Server.HtmlEncode(txtModel.Value.Trim());
                    model.Factory_id = ddlFactory.SelectedValue;
                    model.First_changeover_time = Server.HtmlEncode(txtFirsetCutTime.Text.Trim());
                    model.Try_time = Server.HtmlEncode(txtTryTime.Value.Trim());
                    model.Integrated_operator = Server.HtmlEncode(txtIntegrated.Value.Trim());
                    model.Integrated_unit_id = ddlIntegrated.SelectedValue;
                    model.Product_id = ddlProduct.SelectedValue;
                    model.Station_name = Server.HtmlEncode(txtStation.Text.Trim());
                    model.Supervision_operator = Server.HtmlEncode(txtSupervision.Value.Trim());
                    model.Supervision_unit_id = ddlSupervision.SelectedValue;
                    model.Town_id = hidTown.Value;
                    model.Status_id = ddlStatus.SelectedValue;
                    model.App_main_time = txtAppMaiTime.Text.Trim();
                    model.Con_main_time = txtConMaiTime.Text.Trim();

                    int err = 0;
                    if (txtTryTime.Value.Trim() != "")
                    {
                        try { Convert.ToDateTime(txtTryTime.Value.Trim()); err = 1; }
                        catch { err = 0; }
                        if (err != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：试通时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "wnf", "<script type='text/javascript'>alert('提示信息：试通时间格式错误，请重新输入！');</script>");
                            return;
                        }
                    }
                    if (txtAppMaiTime.Text.Trim() != "")
                    {
                        try { Convert.ToDateTime(txtAppMaiTime.Text.Trim()); err = 1; }
                        catch { err = 0; }
                        if (err != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：申维时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "wnf", "<script type='text/javascript'>alert('提示信息：申维时间格式错误，请重新输入！');</script>");
                            return;
                        }
                    }
                    if (txtConMaiTime.Text.Trim() != "")
                    {
                        try { Convert.ToDateTime(txtConMaiTime.Text.Trim()); err = 1; }
                        catch { err = 0; }
                        if (err != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：成功交维时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "wnf", "<script type='text/javascript'>alert('提示信息：成功交维时间格式错误，请重新输入！');</script>");
                            return;
                        }
                    }
                    if (txtReturnVisit.Value.Trim() != "")
                    {
                        try { Convert.ToDateTime(txtReturnVisit.Value.Trim()); err = 1; }
                        catch { err = 0; }
                        if (err != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：最终回访时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "winf", "<script type='text/javascript'>alert('提示信息：最终回访时间格式错误，请重新输入！');</script>");
                            return;
                        }
                    }
                    if (txtFirsetCutTime.Text.Trim() != "")
                    {
                        try { Convert.ToDateTime(txtFirsetCutTime.Text.Trim()); err = 1; }
                        catch { err = 0; }
                        if (err != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：初次进场割接时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "winf", "<script type='text/javascript'>alert('提示信息：初次进场割接时间格式错误，请重新输入！');</script>");
                            return;
                        }
                    }
                    string tmp="";
                    if (!B_T_C.UpdateInfo(model))
                    {
                        tmp+="割接单信息保存失败！</br>";
                    }
                    if (!B_T_C.UpdateDebugging(hidID.Value, Server.HtmlEncode(txtDebConfTime.Text.Trim()), txtDebErrTimes.Text.Trim(), Server.HtmlEncode(txtDebuggion.Value.Trim())))
                    {
                        tmp+="软调信息保存失败！</br>";
                    }
                    if(!B_T_C.UpdateStatus(hidID.Value,model.Status_id))
                    {
                        tmp+="当前状态保存失败！</br>";
                    }
                    if (!B_T_C.UpdateTest(hidID.Value,Server.HtmlEncode(txtTestTime.Text.Trim()),txtTestErrTimes.Text.Trim()))
                    {
                        tmp+="拨测信息保存失败！</br>";
                    }
                    if (!B_T_C.UpdateDelivery(hidID.Value,txtDeliveryTime.Text.Trim()))
                    {
                        tmp+="交付客户使用时间保存失败！</br>";
                    }
                    if (!B_T_C.UpdateReturnVisit(hidID.Value,txtReturnVisit.Value.Trim()))
                    {
                        tmp+="最终回访时间保存失败！";
                    }
                    if (tmp=="")
                    {
                        tmp="提示信息：保存成功！";
                        lblMsg.ForeColor = Color.DarkGreen;
                        lblMsg.Text = tmp;
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "nf", "<script type='text/javascript'>alert('提示信息：保存成功！');</script>");
                        model = null;
                        GridView1.DataSource = null;
                        GridView2.DataSource = null;
                        LoadData();
                    }
                    else
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = tmp;
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "winf", "<script type='text/javascript'>alert('提示信息：保存失败！');</script>");
                    }
                    
                }
            
        }

        private void LoadData()
        {
            Model_Table_Changeover model;
            model = B_T_C.GetData(Request.Params["id"]);
            txtBusinessOrderNo.Value = model.Order_number;
            ddlProduct.SelectedIndex = ddlProduct.Items.IndexOf(ddlProduct.Items.FindByValue(model.Product_id));
            ddlFactory.SelectedIndex = ddlFactory.Items.IndexOf(ddlFactory.Items.FindByValue(model.Factory_id));
            seleArea.SelectedIndex = seleArea.Items.IndexOf(seleArea.Items.FindByValue(model.Area_id));
            seleTown.SelectedIndex = seleTown.Items.IndexOf(seleTown.Items.FindByValue(model.Town_id));
            txtStation.Text = model.Station_name;
            txtCustomer.Value = model.Customer_name;
            txtCustomerAdd.Value = model.Customer_address;
            txtCustomerTell.Value = model.Customer_tell;
            txtModel.Value = model.Equipment_model;
            ddlSupervision.SelectedIndex = ddlSupervision.Items.IndexOf(ddlSupervision.Items.FindByValue(model.Supervision_unit_id));
            txtSupervision.Value = model.Supervision_operator;
            ddlIntegrated.SelectedIndex = ddlIntegrated.Items.IndexOf(ddlIntegrated.Items.FindByValue(model.Integrated_unit_id));
            txtIntegrated.Value = model.Integrated_operator;
            ddlDebuggion.SelectedIndex = ddlDebuggion.Items.IndexOf(ddlDebuggion.Items.FindByValue(model.Debug_unit_id));
            txtDebuggion.Value = model.Debug_operator;
            txtFirsetCutTime.Text = model.First_changeover_time;
            txtReturnVisit.Value = model.Return_visit_time;
            txtDebConfTime.Text = model.Debugging_confirm_time;
            txtDebErrTimes.Text = model.Debugging_error_times;
            ddlStatus.SelectedIndex = ddlStatus.Items.IndexOf(ddlStatus.Items.FindByValue(model.Status_id));
            txtTestTime.Text = model.Test_confirm_time;
            txtTestErrTimes.Text = model.Test_error_times;
            txtDeliveryTime.Text = model.Delivery_time;
            txtTryTime.Value = model.Try_time;
            txtConMaiTime.Text = model.Con_main_time;
            txtAppMaiTime.Text = model.App_main_time;
            lblDebug.Text = model.Debugging_confirm_timeout;
            lblTest.Text = model.test_confirm_timeout;
            lblDe.Text = model.Delivery_timeout;
            lblReturn.Text = model.Return_visit_timeout;
            GridView1.DataSource = B_T_C.GetProblems("A.changeover_id='" + model.Changeover_id + "' order by problem_strat_time");
            GridView1.DataBind();
            GridView2.DataSource = B_T_C.GetCutLog("A.changeover_id='" + model.Changeover_id + "' order by happened_time desc");
            GridView2.DataBind();
        }
    }
}
